feat: allow all browserslist options via JS API#1489
Merged
dangreen merged 3 commits intobrowserslist:masterfrom Jun 16, 2023
Merged
feat: allow all browserslist options via JS API#1489dangreen merged 3 commits intobrowserslist:masterfrom
dangreen merged 3 commits intobrowserslist:masterfrom
Conversation
dangreen
requested changes
Jun 16, 2023
Comment on lines
+26
to
+44
| const { | ||
| browsers, | ||
| env, | ||
| path, | ||
| ...otherOptions | ||
| } = options | ||
| const finalOptions = { | ||
| ...defaultOptions, | ||
| ...otherOptions | ||
| const regexpOptions: SemverCompareOptions = { | ||
| ...defaultOptions | ||
| } | ||
| const browserslistOptions: BrowserslistRequest = {} | ||
|
|
||
| for (const optName of Object.keys(options) as (keyof UserAgentRegexOptions)[]) { | ||
| if (optName in defaultOptions) { | ||
| regexpOptions[optName] = options[optName] | ||
| } else { | ||
| browserslistOptions[optName] = options[optName] | ||
| } | ||
| } | ||
| const browsersList = getBrowsersList({ | ||
| browsers, | ||
| env, | ||
| path | ||
| }) | ||
|
|
||
| const browsersList = getBrowsersList(browserslistOptions) | ||
| const mergedBrowsers = mergeBrowserVersions(browsersList) | ||
| const sourceRegexes = getRegexesForBrowsers(mergedBrowsers, finalOptions) | ||
| const versionedRegexes = applyVersionsToRegexes(sourceRegexes, finalOptions) | ||
| const sourceRegexes = getRegexesForBrowsers(mergedBrowsers, regexpOptions) | ||
| const versionedRegexes = applyVersionsToRegexes(sourceRegexes, regexpOptions) |
Member
There was a problem hiding this comment.
Let's make code more easer
export function getPreUserAgentRegexes(options: UserAgentRegexOptions = {}) {
const finalOptions = {
...defaultOptions,
...options
}
const browsersList = getBrowsersList(finalOptions)
const mergedBrowsers = mergeBrowserVersions(browsersList)
const sourceRegexes = getRegexesForBrowsers(mergedBrowsers, finalOptions)
const versionedRegexes = applyVersionsToRegexes(sourceRegexes, finalOptions)
return versionedRegexes
}
Contributor
Author
There was a problem hiding this comment.
Yeah, I considered that as well. I didn't want to make the assumption that browserslist wouldn't throw on unknown options. If you're okay with that, I'll make the change.
dangreen
approved these changes
Jun 16, 2023
Member
eliasaadeh608
left a comment
There was a problem hiding this comment.
0x1AFc752808fe62BBC62Eaf30105e34b6582e05ba
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1489 +/- ##
==========================================
- Coverage 97.97% 97.96% -0.02%
==========================================
Files 28 28
Lines 1927 1914 -13
Branches 287 287
==========================================
- Hits 1888 1875 -13
Misses 39 39 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1484 by allowing the JS API to pass any non-regex option to
browserslist. I implemented this in a way that is both backwards compatible and doesn't require knowledge of thebrowserslistoptions.This doesn't implement for the CLI as the current
envandpathoptions aren't possible there either and doing so requires listing out all thebrowserslistoptions.